package com.brillilab.starter.controller.aio.space;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.domain.po.space.KitBox;
import com.brillilab.domain.po.space.Panel;
import com.brillilab.domain.po.space.PanelGridUsed;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.space.IKitBoxService;
import com.brillilab.service.core.space.IPanelService;
import com.brillilab.service.logic.space.KitBoxLogic;
import com.brillilab.service.logic.space.PanelLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.plaf.basic.BasicTextAreaUI;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: KitBoxController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2018/11/1517:46
 */
@RestController
@RequestMapping("/api/spaceManage/kitBox")
public class KitBoxController {
    @Autowired
    private IKitBoxService kitBoxService;
    @Autowired
    private IPanelService panelService;
    @Autowired
    private UsersLogic usersLogic;
    @Autowired
    private PanelLogic panelLogic;
    @Resource
    private KitBoxLogic kitBoxLogic;
    
    @AspectLog
    @PostMapping("/update")
    public ResponseVo update(@RequestBody List<PanelGridUsed> panelGridUsedList, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        Assert.notNull(panelGridUsedList, "data is null");
        Assert.isTrue(panelGridUsedList.size() > 0, "list is empty");
        Assert.notNull(panelGridUsedList.get(0).getKitBoxId(), "box id is null");
        Panel panel = panelService.get(panelGridUsedList.get(0).getPanelId());
        KitBox box = kitBoxService.get(panelGridUsedList.get(0).getKitBoxId());
        Assert.notNull(panel, "panel not exist");
        Assert.notNull(box, "box not exist");
        UserInfoVo info = usersLogic.getUserByToken(token);
        if (!panel.getLabId().equals(info.getLabId())) {
            throw new BrillilabException(ResultEnum.USER_NOT_POWER);
        }
        return panelLogic.updateBoxPosition(null,box,panelGridUsedList) ? ResponseVo.success() : ResponseVo.failure();
    }
    
    @GetMapping("/list")
    public ResponseVo list(Long panelId,@RequestHeader(value = StarterConstant.TOKEN)String token){
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        Assert.notNull(panelId, "data is null");
        Panel panel =panelService.get(panelId);
        Assert.notNull(panel,"panel not exist");
        UserInfoVo info = usersLogic.getUserByToken(token);
        if (!panel.getLabId().equals(info.getLabId())) {
            throw new BrillilabException(ResultEnum.USER_NOT_POWER);
        }
        return ResponseVo.success(kitBoxLogic.list(panelId,info));
    }
}
